const api = require('../../../api/index.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    couponState:[
      {
        type:1,
        text:'可使用',
        num:0,
        id:0,
        active:true
      },
      {
        type: 2,
        text: '已使用',
        num: 0,
        id:1,
        active: false
      },
      {
        type: 3,
        text: '已过期',
        num: 0,
        id:2,
        active: false
      }
    ],
    Coupon:{
      type:1
    },
    dataType:1,
    without:false
  },
  cState:function(e){
    let index = e.currentTarget.dataset.index;
    let that = this;
    this.data.couponState.forEach((item, index)=>{
        let key = `couponState[${index}].active`;
        that.setData({
          [key]:false
        })
    })
    this.setData({
      [`couponState[${index}].active`]:true
    })


    let CouData = {};
    CouData.type = index+1;
    this.setData({
      dataType: index + 1
    })
    this.Cou(CouData);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let types = this.data.couponState[0].type;
    let CouData = {};
    CouData.type = types;
    this.Cou(CouData);
    this.setData({
      imgSrc: api.imgPathUrl
    })
  },
  Cou:function(data){
    let thit = this;
    this.setData({
        Coupon:'' 
    })
    api.Coupon(data, (res) => {
      console.log(res);
      if (res.data.result.coupon.length === 0 ){
        thit.setData({
          without: true
        })  
      }else{
        thit.setData({
          without: false
        }) 
      }
      // console.log(this.data.couponState[0].num)
      thit.data.couponState[0].num = res.data.result.count.incount;
      thit.data.couponState[1].num = res.data.result.count.uncount;
      thit.data.couponState[2].num = res.data.result.count.pastcount;
       this.setData({
         Coupon: res.data.result.coupon,
         couponState: thit.data.couponState
       })
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})
